package com.damai.hotload;

import com.citywithincity.api.ApiFilter;

public class App{

	private ClassLoader loader;
	
	
	IHotModule service;
	
	public App(ClassLoader loader){
		this.loader = loader;
	}
	

	
	public void start() throws HotLoadException{
		try {
			Class<?> clazz = loader.loadClass("com.czc.main.Main");
			Object value = clazz.newInstance();
			if(value instanceof IHotModule){
				IHotModule service = (IHotModule) value;
				service.start(ApiFilter.me());
				this.service = service;
			}else{
				throw new RuntimeException("Not module");
			}
			
		} catch (ClassNotFoundException e) {
			throw new HotLoadException(e);
		} catch (InstantiationException e) {
			throw new HotLoadException(e);
		} catch (IllegalAccessException e) {
			throw new HotLoadException(e);
		} catch (HotLoadException e) {
			throw e;
		}
	}

	
	public void stop() {
		if(service!=null){
			service.stop(ApiFilter.me());
			service = null;
		}
		
	}

}
